<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
echo "<h2>Каталог товаров</h2>";
echo "<p><a href='/admin/tools/cache/'>Скинуть кэш карты сайта</a></p>";
$this->table->set_heading('id', 'Название', 'Фотографии', 'Сортировка', 'Действия');
$tmpl = array('table_open' => '<table border="0" cellpadding="0" cellspacing="0" class="admin_table">');
$this->table->set_template($tmpl);

$class = 'catalog';
if ($this->users_rights_model->check_rights($class, 'block', true)) $canblock = 1;
if ($this->users_rights_model->check_rights($class, 'unblock', true)) $canblock = 1;
if ($this->users_rights_model->check_rights($class, 'edit', true)) $canedit = 1;
if ($this->users_rights_model->check_rights($class, 'delete', true)) $candelete = 1;
if ($this->users_rights_model->check_rights($class, 'add', true)) $canadd = 1;

if ($active_parent != 0) $this->table->add_row('', '<img src="/data/images/icons/up.png" alt="Уровень вверх" title="Уровень вверх" style="margin: 0px; position: relative; top: 3px;" /> <a href="/admin/catalog/index/'.$active['parent_parent'].'/">На один уровень выше</a>', '', '', '');
if (sizeof($pages) == 0 && sizeof($items) == 0) $this->table->add_row('', array('data' => '<br />Категория пуста<br /><br />', 'colspan' => 4));
$d = 0;
if (sizeof($pages) > 0) {
	foreach ($pages as $page) {
		$d++;
		$this->table->add_row(
				$page['parent_id'],
				"<img src='/data/images/icons/folder.png' alt='Категория' title='Категория' style='position: relative; top: 3px; margin: 0px;' /> <a href='/admin/catalog/index/".$page['parent_id']."/'>".$page['parent_title']."</a>",
				array('data' => ($page['parent_image'] != ""?"<div style='position: relative;'><img src='/data/images/icons/image.png' alt='".$page['parent_title']."' id='parent_".$page['parent_id']."' rel='/uploads/catalog_parents/".$page['parent_id']."/small/".$page['parent_image']."' class='small_preview' /></div>":""), 'style' => ''),
				array('data' => "<input class='ordering' type='text' tabindex='".$d."' value='".$page['parent_order']."' name='order[parent][".$page['parent_id']."]' />", 'style' => 'text-align: center;'),
				showmeactions(
						$class,
						'parent/'.$page['parent_id'],
						$page['parent_status'],
						$canedit, $candelete, $canblock,
						null
					)
			);
	}
}
if (sizeof($items) > 0) {
	foreach ($items as $item) {
		$photos = $this->db->select()->from('catalog_images')->where('photo_item', $item['item_id'])->order_by('photo_order', 'asc')->get()->result_array();
		$images = "<div style='position: relative;'>";
		foreach ($photos as $photo) {
			$images .= "<img src='/data/images/icons/image".($photo['photo_main']==1?"_main":"").".png' alt='".$item['item_title']."' id='item_".$item['item_id']."' rel='/uploads/catalog_items/".$item['item_id']."/small/".$photo['photo_file']."' class='small_preview' />";
		}
		$images .= "</div>";
//		$images = "<div style='position: relative;'><img src='/data/images/icons/image.png' alt='".$page['parent_title']."' id='parent_".$page['parent_id']."' rel='/uploads/catalog_parents/".$page['parent_id']."/small/".$page['parent_image']."' class='small_preview' /></div>";
		$d++;
		$this->table->add_row(
				$item['item_id'],
				"<img src='/data/images/icons/box.png' alt='Товар' title='Товар' style='position: relative; top: 3px; margin: 0px;' /> <a href='/admin/catalog/edit/item/".$item['item_id']."/'>".$item['item_title']."</a>",
				$images,
				array('data' => "<input class='ordering' type='text' tabindex='".$d."' value='".$item['item_order']."' name='order[item][".$item['item_id']."]' />", 'style' => 'text-align: center;'),
				showmeactions(
						$class,
						'item/'.$item['item_id'],
						$item['item_status'],
						$canedit, $candelete, $canblock,
						null
					)
			);
	}
}

$d++;
if (sizeof($pages) > 0 || sizeof($items) > 0)
	$this->table->add_row(
			"","","",
			array('data' => "<input type='submit' tabindex='".$d."' class='ordering' value='Сохранить' />", 'style' => 'text-align: center; width: 80px;'),
			""
		);

echo "<div style='margin: 5px 0;'><a href='/admin/catalog/'>Каталог товаров</a> ".$crumps."</div>";
echo "<form action='/admin/catalog/order/".$active_parent."/' method='post'>";
echo $this->table->generate();
echo "</form>";
echo $paging;
if ($canadd == 1) {
	if((sizeof(explode("&", $active['parent_path'])) - 1) < 2){
		echo showmeadd($class, 'Добавить категорию', 'parent', $active_parent);
	}
	echo showmeadd($class, 'Добавить товар', 'item', $active_parent);
}
echo "<div class='cc'></div>";
?>
